package kk.servlet;

import kk.domain.User;
import kk.service.UserService;
import kk.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registServlet")
public class RegistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        User user=new User(username,password,email);
        UserService userService=new UserServiceImpl();
        boolean registOk = userService.regist(user);
        if (registOk){
            //注册成功
            request.getRequestDispatcher("/pages/user/regist_success.jsp").forward(request,response);

        }else {
            //注册失败,保存信息，跳转到regist.jsp
            request.setAttribute("msg","用户名已存在");
            request.setAttribute("username",username);
            request.setAttribute("email",email);
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
